﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

namespace HaiFang
{
    /// <summary>
    /// Summary description for UserRightsHandler
    /// </summary>
    public class UserRightsHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            if (!String.IsNullOrEmpty(context.Request.QueryString["action"]))
            {
                string action = context.Request.QueryString["action"];
                switch (action)
                {
                    case "GetUserRights":
                        {
                            string Params = context.Request.QueryString["Params"];
                            ResponseGetUserRights(context, Params);
                        }
                        break;
                    case "GetUserRightsByUserID":
                        {
                            string Params = context.Request.QueryString["Params"];
                            ResponseGetUserRightsByUserID(context, Params);
                        }
                        break;
                    default:
                        WebHelper.WriteJsonError(context, "未知的操作.");
                        break;
                }
            }
            else if (context.Request.Form.Count > 0)
            {
                string action = context.Request.Form["action"];
                switch (action)
                {
                    case "SaveUserRights":
                        {
                            string Params = context.Request.Form["Params"];
                            if (!string.IsNullOrEmpty(Params))
                            {
                                ResponseSaveUserRights(context, Params);
                            }
                        }
                        break;
                    default:
                        WebHelper.WriteJsonError(context, "未知的操作");
                        break;
                }
            }
        }

        private void ResponseGetUserRights(HttpContext context, string Params)
        {
            try
            {
                IList<UserRightsTree> list = ProxyUserRights.QueryUserRightsTree(WebHelper.GetCurrentUserID(), WebHelper.GetCurrentUserProjectID(), WebHelper.GetCurrentUserID());
                WebHelper.WriteJsonResult(context, list);
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
                WebHelper.WriteJsonError(context, null);
            }
        }

        private void ResponseGetUserRightsByUserID(HttpContext context, string Params)
        {
            try
            {
                IList<UserRightsTree> list = ProxyUserRights.QueryUserRightsTree(WebHelper.GetCurrentUserID(), WebHelper.GetCurrentUserProjectID(), Convert.ToInt32(Params));
                WebHelper.WriteJsonResult(context, list);
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
                WebHelper.WriteJsonError(context, null);
            }
        }

        private void ResponseSaveUserRights(HttpContext context, string Params)
        {
            try
            {
                int ErrorMSG = 0;
                UserRightsWithUser r = null;
                if (!String.IsNullOrEmpty(Params))
                {
                    r = JsonHelper.JsonDeserialize<UserRightsWithUser>(Params);
                    if (r != null)
                    {
                        if (r.UserInfoWithGroup != null)
                        {
                            string RightIDStr=string.Empty;
                            if (r.UserRights != null)
                            {
                                foreach (UserRights ur in r.UserRights)
                                {
                                    RightIDStr += string.Format("{0},{1};", ur.RightFlag, ur.RightID);
                                }
                            }
                            ProxyUserRights.InsertUserRights(WebHelper.GetCurrentUserID(), r.UserInfoWithGroup.AutoID, RightIDStr);
                        }

                        WebHelper.WriteJsonResult(context, ErrorMSG);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            WebHelper.WriteJsonError(context, null);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}